﻿using System;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Framework;
using Kaleida.ServiceMonitor.Model.Runtime;

namespace Kaleida.ServiceMonitor.Model.ScriptDirectives
{
	public class MaxErrorAgeDirective : IScriptDirective
	{
		private readonly Duration maxAge;

		public MaxErrorAgeDirective([NotNull] string interval)
        {
			if (interval == null) throw new ArgumentNullException("interval");

			maxAge = interval.ToDuration();

			if (maxAge.ToTimeSpan().Ticks <= 0)
                throw new InvalidOperationException(string.Format("{0} is not a valid error duration. Must be greater than zero", maxAge));
        }

        public CompiledNode Apply([NotNull] CompiledScriptModule module)
        {
            if (module == null) throw new ArgumentNullException("module");

			module.MaximumErrorAge = maxAge;
            return new CompiledNode(module);
        }
    }
}
